﻿using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace Portal.Controllers
{
    public class FileController : Controller
    {
        private readonly IWebHostEnvironment _hostEnvironment;

        public FileController(IWebHostEnvironment hostEnvironment)
        {
            _hostEnvironment = hostEnvironment;
        }

        /// <summary>
        /// 根据路径获取图片 返回图片的方式
        /// </summary>
        /// <returns></returns>
        public IActionResult ShowImageByPath(string path)
        {
            var fileName = Path.Combine(_hostEnvironment.WebRootPath, @"resources\images", "layui.png");

            //1. 直接返回图片
            return PhysicalFile(fileName, "image/jpeg");

           


        }

        /// <summary>
        /// 根据请求路径获取图片 文件流的方式
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> ShowImage()
        {
            var fileName = Path.Combine(_hostEnvironment.WebRootPath, @"resources\images", "layui.png");

            await using var sw = new FileStream(fileName, FileMode.Open);

            var bytes = new byte[sw.Length];
            sw.Read(bytes, 0, bytes.Length);
            sw.Close();
            return new FileContentResult(bytes, "image/jpeg");
        }
    }
}